package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbTypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("TbTypeTemplate")
public class TbTypeTemplateController {

    //注入service
    @DubboReference
    private TbTypeTemplateService tbTypeTemplateService;

    //查询商品类型模板全部数据(要求分页)
    @ResponseBody
    @RequestMapping("TbTypeTemplateshowall")
    public Result TbTypeTemplateshowall(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize, String name) {
        try {
            PageResult<TbTypeTemplate> result = tbTypeTemplateService.TbTypeTemplateshowall(pageNum, pageSize, name);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(result);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //查询商品类型模板全部数据
    @ResponseBody
    @RequestMapping("TbTypeTemplateshow")
    public Result TbTypeTemplateshow() {
        try {
            List<TbTypeTemplate> result = tbTypeTemplateService.TbTypeTemplateshow();
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(result);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //添加商品类型模板
    @ResponseBody
    @RequestMapping("addTbTypeTemplate")
    public Result addTbTypeTemplate(TbTypeTemplate tbTypeTemplate) {
        try {
            tbTypeTemplateService.addTbTypeTemplate(tbTypeTemplate);
            return Result.response(ResultEnums.ADD_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.ADD_ERROR);
        }
    }

    //回显单个商品类型模板
    @ResponseBody
    @RequestMapping("showecho")
    public Result showecho(Long id) {
        try {
            TbTypeTemplate tbTypeTemplate = tbTypeTemplateService.showecho(id);
            return Result.response(ResultEnums.ECHO_SUCCESS).setData(tbTypeTemplate);
        } catch (Exception e) {
            return Result.response(ResultEnums.ECHO_ERROR);
        }
    }

    //修改商品类型模板数据
    @ResponseBody
    @RequestMapping("updateTbTypeTemplate")
    public Result updateTbTypeTemplate(TbTypeTemplate tbTypeTemplate) {
        try {
            tbTypeTemplateService.updateTbTypeTemplate(tbTypeTemplate);
            return Result.response(ResultEnums.UPDATE_SUCCESS);
        } catch (Exception e) {
            System.out.println(e);
            return Result.response(ResultEnums.UPDATE_ERROR);
        }
    }

    //删除单个数据
    @ResponseBody
    @RequestMapping("deleteTbTypeTemplate")
    public Result deleteTbTypeTemplate(Long id) {
        try {
            tbTypeTemplateService.deleteTbSpecification(id);
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }

    //删除多个数据
    @ResponseBody
    @RequestMapping("deleteallTbTypeTemplate")
    public Result deleteallTbTypeTemplate(@RequestParam(value = "ids[]") List<Long> ids) {
        try {
            tbTypeTemplateService.deleteallTbTypeTemplate(ids);
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }
}
